package com.warehouse.module.business.controller.admin.cargoplan;

import com.warehouse.framework.apilog.core.annotation.ApiAccessLog;
import com.warehouse.framework.common.pojo.CommonResult;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.excel.core.util.ExcelUtils;
import com.warehouse.module.business.controller.admin.cargoinfo.vo.CargoInfoPageReqVO;
import com.warehouse.module.business.controller.admin.cargoinfo.vo.CargoInfoRespVO;
import com.warehouse.module.business.controller.admin.cargoinfo.vo.CargoInfoSaveReqVO;
import com.warehouse.module.business.controller.admin.cargoplan.vo.CancelPlanVO;
import com.warehouse.module.business.controller.admin.cargoplan.vo.CargoPlanExcelRespVO;
import com.warehouse.module.business.controller.admin.cargoplan.vo.CargoPlanPageReqVO;
import com.warehouse.module.business.dal.dataobject.cargoplan.CargoPlanDO;
import com.warehouse.module.business.service.cargoinfo.CargoInfoService;
import com.warehouse.module.business.service.cargoplan.CargoPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static com.warehouse.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.warehouse.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 计划")
@RestController
@RequestMapping("/business/cargo-plan")
@Validated
public class CargoPlanController {

    @Resource
    private CargoPlanService cargoPlanService;

    @GetMapping("/page")
    @Operation(summary = "获得计划分页")
    @PreAuthorize("@ss.hasPermission('business:cargo-plan:query')")
    public CommonResult<PageResult<CargoPlanDO>> getCargoPlanPage(@Valid CargoPlanPageReqVO pageReqVO) {
        return success(cargoPlanService.pageCargoPlans(pageReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得计划详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:cargo-plan:query')")
    public CommonResult<List<CargoInfoRespVO>> getCargoInfo(@RequestParam("id") Long id) {
        return success(cargoPlanService.listCargoInfosByPlanId(id));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计划")
    @PreAuthorize("@ss.hasPermission('business:cargo-plan:update')")
    public CommonResult<Boolean> updateCargoInfo(@Valid @RequestBody CancelPlanVO cancelPlanVO) {
        cargoPlanService.editPlan(cancelPlanVO);
        return success(true);
    }

    @GetMapping("/download-excel")
    @Operation(summary = "下载计划历史 Excel")
    @PreAuthorize("@ss.hasPermission('business:cargo-plan:download')")
    @ApiAccessLog(operateType = EXPORT)
    public void downloadCargoPlanExcel(@RequestParam("id") Long id,
              HttpServletResponse response) throws IOException {
        List<CargoPlanExcelRespVO> list = cargoPlanService.getCargoPlanExcel(id);
        // 导出 Excel
        ExcelUtils.write(response, "计划历史.xlsx", "数据", CargoPlanExcelRespVO.class, list);
    }
}
